<template>
	<view>
		<uni-nav-bar leftIcon="arrow-left" rightText="搜索" leftWidth="40rpx" rightWidth="60rpx" fixed="true"
			@clickLeft="back()" @clickRight="search(this.keywords || this.realkeyword)" statusBar>
			<uni-search-bar radius="100" :placeholder="realkeyword" clearButton="none" cancelButton="none" :focus="true"
				v-model="keywords" @confirm="search" />
		</uni-nav-bar>
		<view class="history" v-if="history[0]">
			历史
			<scroll-view scroll-x="true">
				<uni-tag :text="item" circle="true" v-for="(item,i) of history" :key="i" @click="search(item)">
				</uni-tag>
			</scroll-view>
			<uni-icons type="trash-filled" @click="removeHistory"></uni-icons>
		</view>
		<view class="hots">
			<text class="hots-title">热搜榜</text>
			<view class="hots-list">
				<view class="" v-for="(item,i) of hots" @click="search(item.first)">
					<text :class="i<3 ? 'hots-num red' : 'hots-num'">{{i+1}}</text>
					<text :class="i<3 ? 'hots-text bold' : 'hots-text'">{{item.first}}</text>
				</view>
			</view>
		</view>
		<player v-if="detail"></player>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex' //引入mapState
	export default {
		data() {
			return {
				keywords: '',
				hots: []
			}
		},
		computed: mapState([
			'url',
			'realkeyword',
			'detail',
			'history'
		]),
		methods: {
			...mapMutations(['setRealkeyword', 'setHistory', 'removeHistory', 'restoreHistory']),
			...mapActions(['getRealkeyword']),
			back() {
				uni.navigateTo({
					url: '../index/index',
				});
			},
			search(keywords) {
				this.setHistory(keywords)
				uni.navigateTo({
					url: `../single/single?keywords=${keywords}`,
				});
			}
		},
		created() {
			this.getRealkeyword(),
				uni.getStorage({
					key: 'history',
					success: (res) => {
						this.restoreHistory(res.data)
					}
				});
			uni
				.request({
					url: this.url + "/search/hot"
				})
				.then((res) => {
					// console.log(res.data.result.hots);
					this.hots = res.data.result.hots
				})
				.catch((err) => {
					console.error(err);
				});
		}
	}
</script>

<style lang="scss">
	.uni-searchbar {
		width: 100%;
		padding: 0;
		align-items: center;
	}

	.history {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 80rpx;
		margin: 0 20rpx;
		font-weight: bold;

		scroll-view {
			width: 500rpx;
			// height: 80rpx;
			// line-height: 80rpx;
			white-space: nowrap;

			.uni-tag {
				display: inline-block;
				margin-right: 10rpx;
			}
		}
	}
	//热搜榜
	.hots {
		margin: 0 20rpx;

		.hots-title {
			font-weight: bold;
		}

		.hots-list {
			display: flex;
			flex-wrap: wrap;
			font-size: 24rpx;
			line-height: 50rpx;

			view {
				display: flex;
				width: 355rpx;

				.hots-num {
					width: 30rpx;
				}

				.hots-num.red {
					color: red;
				}

				.hots-text.bold {
					font-weight: bold;
				}
			}
		}
	}
</style>
